package com.yupi.goj.model.vo;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yupi.goj.model.dto.question.JudgeCase;
import com.yupi.goj.model.dto.question.JudgeConfig;
import com.yupi.goj.model.entity.Question;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 题目
 * @TableName question
 */
@Data
public class QuestionUpdateVO {
    /**
     * id
     */
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表（json 数组）
     */
    private List<String> tags;

    /**
     * 题目答案
     */
    private String answer;

    /**
     * 题目提交数
     */
    private Integer submitNum;

    /**
     * 题目通过数
     */
    private Integer acceptedNum;

    /**
     * 判题用例（json 数组）
     */
    private List<JudgeCase> judgeCase;

    /**
     * 判题配置（json 对象）
     */
    private JudgeConfig judgeConfig;

    /**
     * 点赞数
     */
    private Integer thumbNum;

    /**
     * 收藏数
     */
    private Integer favourNum;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;
    /**
     * 创建题目人的信息
     */
    private UserVO userVO;

    /**
     * 包装类转对象
     *
     * @param questionUpdateVO
     * @return
     */
    public static Question voToObj(QuestionUpdateVO questionUpdateVO) {
        if (questionUpdateVO == null) {
            return null;
        }
        Question question = new Question();
        // 源对象和目标对象的属性类型不一致时，该属性不会被复制
        BeanUtils.copyProperties(questionUpdateVO, question);
        List<String> tagList = questionUpdateVO.getTags();
        if (tagList != null) {
            question.setTags(JSONUtil.toJsonStr(tagList));
        }
        JudgeConfig voJudgeConfig =  questionUpdateVO.getJudgeConfig();
        if (voJudgeConfig != null) {
            question.setJudgeConfig(JSONUtil.toJsonStr(voJudgeConfig));
        }
        List<JudgeCase> judgeCaseList = questionUpdateVO.getJudgeCase();
        if (judgeCaseList != null) {
            question.setJudgeCase(JSONUtil.toJsonStr(judgeCaseList));
        }
        return question;
    }

    /**
     * 对象转包装类
     *
     * @param question
     * @return
     */
    public static QuestionUpdateVO objToVo(Question question) {
        if (question == null) {
            return null;
        }
        QuestionUpdateVO questionUpdateVO = new QuestionUpdateVO();
        BeanUtils.copyProperties(question, questionUpdateVO);
        List<String> tagList = JSONUtil.toList(question.getTags(), String.class);
        questionUpdateVO.setTags(tagList);
        String judgeConfig = question.getJudgeConfig();
        questionUpdateVO.setJudgeConfig(JSONUtil.toBean(judgeConfig, JudgeConfig.class));
        List<JudgeCase> judgeCaseList = new ArrayList<>();
        String judgeCaseStr = question.getJudgeCase();
        if (JSONUtil.isTypeJSONArray(judgeCaseStr)) {
            JSONArray jsonArray = JSONUtil.parseArray(judgeCaseStr);
            for (Object obj: jsonArray) {
                JSONObject jsonObject = (JSONObject) obj;
                JudgeCase judgeCase = jsonObject.toBean(JudgeCase.class);
                judgeCaseList.add(judgeCase);
            }
        }
        questionUpdateVO.setJudgeCase(judgeCaseList);
        return questionUpdateVO;
    }

    private static final long serialVersionUID = 1L;
}